import { observable, action } from 'mobx';
import { WeaTableNew, WeaForm } from 'comsMobx';
import { ComsStore } from './tables/common';
import * as API_DUMMY from '../apis/dummy';
import { Modal, message } from 'antd';

// const confirm = Modal.confirm;
import { WeaLocaleProvider } from 'ecCom';

const getLabel = WeaLocaleProvider.getLabel;

export class DocDummyStore extends ComsStore {
  @observable title = getLabel(20482, '虚拟目录');
  @observable key = '';
  @observable selectedTreeKeys = [];
  @observable dummyId = '';
  @observable expandedKey = [];
  @observable showSearchAd = false;
  @observable formImport = new WeaForm();
  @observable chooseDummyCon = [];
  @observable chooseDummyRM = {};
  @observable dialogCopy = {
    title: getLabel(20487, '导入到虚拟目录'),
    icon: 'icon-coms-doc',
    iconBgcolor: '#F14A2D',
    style: { width: '500px' },
  };
  @observable visible = false;
  @observable copyOrMove = false; // false为copy
  @observable treeData = [];
  @observable tabData = [];
  @observable quickSearchValue = '';

  @action

  setTitle = (title) => {
    this.title = title;
  }
  setKey(key) {
    this.key = key;
  }
  setSelectedTreeKeys = (key, shouldExpandedKey) => {
    this.comsWeaTableStore.setSelectedRowKeys([]);
    this.selectedTreeKeys = key;
    if (key.length !== 0) {
      this.dummyId = this.getKey();
      this.doSearch('Dummy', { dummyId: this.dummyId });
      this.getRightMenu('Dummy', { dummyId: this.dummyId });
      this.form.updateFields({ docsubject: { value: '' } });
      this.quickSearchValue = '';
      // 左侧树展开，只需在点击标题导航执行以下代码，即shouldExpandedKey为true
      let tempExpandedkey = [];
      this.treeData.forEach((item) => {
        tempExpandedkey.push(item.domid);
        if (item.domid === key[0]) {
          this.setTabData([item]);
          if (shouldExpandedKey) this.setExpandedKey([]);
        } else if (item.haschild) {
            const tempResult = this.getItem(item, key);
            tempResult.forEach((iteml) => {
              if (iteml.getNode) {
                let temptreeData = [];
                temptreeData = this.getShowItem(tempResult, iteml.item.domid, iteml.item.pid);
                this.setTabData(temptreeData);
                temptreeData.forEach((i) => {
                  tempExpandedkey.push(i.domid);
                });
                if (shouldExpandedKey) this.setExpandedKey(tempExpandedkey);
              }
            });
          }
      });
    }
  }
  setDummyId = (key) => {
    this.dummyId = key;
  }
  setShowSearchAd = (showSearchAd) => {
    this.showSearchAd = showSearchAd;
  }
  setVisible = (visible) => {
    this.visible = visible;
  }
  setCopyOrMove = (copyOrMove) => {
    this.copyOrMove = copyOrMove;
  }
  setTabData = (tabData) => {
    this.tabData = tabData;
  }
  setExpandedKey = (expandedKey) => {
    this.expandedKey = expandedKey;
  }
  setQuickSearchValue = (quickSearchValue) => {
    this.quickSearchValue = quickSearchValue;
  }

  copyOfDummy = (params) => {
    API_DUMMY.copyOfDummy(params).then((data) => {
      this.visible = false;
      // confirm({
      //   content: (
      //     <span>{data.msg}</span>
      //   )
      // });
      if (data.status === 1) {
        message.success(data.msg);
        this.doSearch('Dummy', { dummyId: this.dummyId });
        this.formImport.reset();
      } else {
        message.error(data.msg);
      }
    });
  }
  moveOfDummy = (params) => {
    API_DUMMY.moveOfDummy(params).then((data) => {
      this.visible = false;
      // confirm({
      //   content: (
      //     <span>{data.msg}</span>
      //   )
      // });
      if (data.status === 1) {
        message.success(data.msg);
        this.doSearch('Dummy', { dummyId: this.dummyId });
        this.formImport.reset();
      } else {
        message.error(data.msg);
      }
    });
  }
  removeOfDummy = (params) => {
    API_DUMMY.removeOfDummy(params).then((data) => {
    });
  }
  getchooseDummy = (params) => {
    API_DUMMY.chooseDummy(params).then((data) => {
      this.chooseDummyCon = data.condition;
      this.formImport.initFormFields(this.chooseDummyCon);
      this.chooseDummyRM = data.rightMenu;
    });
  }
  getImportFormParams = () => this.formImport.getFormParams()
  getTreeData = (params) => {
    API_DUMMY.getTreeData(params).then((data) => {
      this.treeData = data.treedata;
      let currentData = null;
      if (this.dummyId) {
        currentData = this.getItemByKey(this.dummyId);
      }
      if (currentData == null) {
        currentData = data.treedata[0];
      }

      this.setSelectedTreeKeys([currentData.domid], true);
      this.setKey([this.dummyId]);
      this.doSearch('Dummy', { dummyId: this.dummyId });
      this.getRightMenu('Dummy', { dummyId: this.dummyId });
    });
  }

  getItem = (item, selected) => {
    let getNode = false;
    let returnItem = [];
    if (item.domid === selected[0]) {
      getNode = true;
      returnItem.push({ item, getNode });
    } else {
      returnItem.push({ item, getNode });
      if (item.haschild) {
        item.childs.forEach((i) => {
          let tempResult = [];
          tempResult = this.getItem(i, selected);
          returnItem = returnItem.concat(tempResult);
        });
      }
    }
    return returnItem;
  }
  getShowItem = (tempResult, domid, pid) => {
    let result = [];
    let temp = [];
    tempResult.forEach((item) => {
      if (item.item.domid === domid) {
        result.unshift(item.item);
      }
    });
    tempResult.forEach((item) => {
      if (item.item.key === pid) {
        result.unshift(item.item);
        temp = [];
        if (pid !== '') {
          temp = this.getShowItem(tempResult, item.item.domid, item.item.pid);
          temp.pop();
          result = temp.concat(result);
        }
      }
    });
    return result;
  }
  getKey = () => {
    let keySearch = '';
    const key = this.selectedTreeKeys[0];
    this.treeData.forEach((item) => {
      if (item.domid === key) {
        keySearch = item.key;
      } else if (item.haschild) {
          const tempkey = this.getItem(item, this.selectedTreeKeys);
          tempkey.forEach((i) => {
            if (i.getNode) {
              keySearch = i.item.key;
            }
          });
        }
    });
    return keySearch;
  }

  getItemByKey = (dummyId) => {
    let currentItem = null;
    this.treeData.forEach((item) => {
      if (item.key == dummyId) {
        currentItem = item;
      } else if (item.haschild) {
          const tempkey = this.getChildItemByKey(item, dummyId);
          tempkey.forEach((i) => {
            if (i.getNode) {
              currentItem = i.item;
            }
          });
        }
    });
    return currentItem;
  }

  getChildItemByKey = (item, dummyId) => {
    let getNode = false;
    let returnItem = [];
    if (item.key === dummyId) {
      getNode = true;
      returnItem.push({ item, getNode });
    } else {
      returnItem.push({ item, getNode });
      if (item.haschild) {
        item.childs.forEach((i) => {
          let tempResult = [];
          tempResult = this.getChildItemByKey(i, dummyId);
          returnItem = returnItem.concat(tempResult);
        });
      }
    }
    return returnItem;
  }
}

